﻿var AuditTypeVm = function(jsonOfObject) {
    var self = this;

    self.Reasons = ko.observableArray([]).extend({
        trackChange: true
    });

    self.IsReasonTableVisible = ko.observable(false);

    self.Refresh = function (jsonData) {
        self.Reasons.removeAll();

        var mapping = {
            'Reasons': {
                create: function (options) {
                    return new AuditReasonVm(options.data);
                }
            }
        }

        ko.mapping.fromJS(jsonData, mapping, self);
    };

    self.AreThereReasons = ko.pureComputed(function() {
        var count = self.Reasons().length;
        return count > 0;
    });

    self.AreAuditReasonsVisible = ko.pureComputed(function() {
        return self.AreThereReasons() && self.IsReasonTableVisible();
    });

    self.OnAfterAddAuditReasonToAuditType = function (arg) {
        self.IsReasonTableVisible(true);
    };

    self.Refresh(jsonOfObject);
    self.DirtyFlag = ko.dirtyFlag(self, false);
};